package com.china08.yxyapi.api.home.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter.ResSubjectPhase;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.mg.YxyUserCenterRepository;

@CrossOrigin
@RestController
public class InitController extends YxyApiBaseController {

    @Autowired
    private YxyResBookRepository yrbr;

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @Autowired
    private YxyUserCenterRepository yxyUserCenterRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/res/init", method = RequestMethod.GET)
    public RespModel find(HttpServletRequest request, HttpServletResponse response)
            throws IllegalAccessException, InvocationTargetException {
        RespModel model = new RespModel();
        // ##
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(schoolId)) {
            // ## 获得当前用户角色 上次操作的 学段，学科 ，版本
            YxyUserCenter center = yxyUserCenterRepository.findByUserId(userId);
            if (center != null) {
                ResSubjectPhase lastRes = center.getLastWeikeView();
                if (lastRes != null) {
                    model.phase = lastRes.getPhaseId();
                    model.subject = lastRes.getSubjectId();
                    model.version = lastRes.getVersionId();
                    model.phaseName = lastRes.getPhaseName();
                    model.versionName = lastRes.getVersionName();
                    model.subjectName = lastRes.getSubjectName();
                }
            }
        }

        YxyResBook YxyResBook = yrbr.findTop1BySubjectIdAndVersionIdAndPhaseIdAndStatus(model.subject, model.version,
                model.phase, 1);
        model.semester = new SemesterResp();
        if (YxyResBook != null) {
            BeanUtils.copyProperties(model.semester, YxyResBook);
        }
        if (model.semester != null) {
            List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(model.semester.getId(),
                    1);
            if (listcoContents.size() > 0) {
                BeanUtils.copyProperties(model.bookContent, listcoContents);
            }
        }
        model.setMessage("成功");
        return model;
    }

    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    public static class RespModel extends YxyApiRespModel {

        public String phaseName = "初中";

        public String subjectName = "物理";

        public String versionName = "人教课标版";

        public String phase = "02";

        public String subject = "jcsub02";

        public String version = "bb08";

        public SemesterResp semester;

        public List<BookContentRespModel> bookContent;

        public List<BookContentRespModel> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel> bookContent) {
            this.bookContent = bookContent;
        }

    }

    public static class SemesterResp {

        private String id;

        // 科目
        private String subjectId;

        private String subjectName;

        // 版本
        private String versionId;

        private String versionName;

        // 学期
        private String semersterId;

        private String semersterName;

        // 学段
        private String phaseId;

        private String phaseName;
        // 状态信息0无效 1 有效
        private Integer status = 1;

        /**
         * 
         */
        private Date pulbicationDate;

        /**
         * 教材封面
         */
        private String cover;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getSubjectId() {
            return subjectId;
        }

        public void setSubjectId(String subjectId) {
            this.subjectId = subjectId;
        }

        public String getSubjectName() {
            return subjectName;
        }

        public void setSubjectName(String subjectName) {
            this.subjectName = subjectName;
        }

        public String getVersionId() {
            return versionId;
        }

        public void setVersionId(String versionId) {
            this.versionId = versionId;
        }

        public String getVersionName() {
            return versionName;
        }

        public void setVersionName(String versionName) {
            this.versionName = versionName;
        }

        public String getSemersterId() {
            return semersterId;
        }

        public void setSemersterId(String semersterId) {
            this.semersterId = semersterId;
        }

        public String getSemersterName() {
            return semersterName;
        }

        public void setSemersterName(String semersterName) {
            this.semersterName = semersterName;
        }

        public String getPhaseId() {
            return phaseId;
        }

        public void setPhaseId(String phaseId) {
            this.phaseId = phaseId;
        }

        public String getPhaseName() {
            return phaseName;
        }

        public void setPhaseName(String phaseName) {
            this.phaseName = phaseName;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public Date getPulbicationDate() {
            return pulbicationDate;
        }

        public void setPulbicationDate(Date pulbicationDate) {
            this.pulbicationDate = pulbicationDate;
        }

        public String getCover() {
            return cover;
        }

        public void setCover(String cover) {
            this.cover = cover;
        }

    }

}
